package com.bci.pwtz.admin.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.bci.pwtz.admin.service.AdminUserService;
import com.bci.pwtz.common.util.PaginationList;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.dao.AdminUserMapper;
import com.bci.pwtz.mysql.model.AdminUser;

/**
 * 一句话描述 <br/>
 * <p>
 * Description:<br/>
 * <p>
 * TODO 详细描述
 * 
 * <p>
 * For Examples <br/>
 * 
 * <PRE>
 *      TODO 代码使用示例
 * </PRE>
 * <p>
 */
@Service
public class AdminUserServiceImpl implements AdminUserService
{

    private AdminUserMapper adminUserMapper;
    
    private JdbcTemplate jdbcTemplate;
    
    
    /**
     * @return the adminUserMapper
     */
    public AdminUserMapper getAdminUserMapper()
    {
        return adminUserMapper;
    }

    /**
     * @param adminUserMapper the adminUserMapper to set
     */
    public void setAdminUserMapper(AdminUserMapper adminUserMapper)
    {
        this.adminUserMapper = adminUserMapper;
    }

    private static final Logger _logger = Logger.getLogger(AdminUserServiceImpl.class);

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param userId
     * 
     * @return
     * 
     * @throws CourtDatabaseException
     * 
     * @see com.bcinfo.alumni.service.BranchService#delete(long)
     */
    @Override
    public int delete(Long userId) throws PwtzDatabaseException
    {
        int result = 0;
        if (_logger.isInfoEnabled())
        {
            _logger.info("delete:userId=" + userId);
        }
        try
        {
            result = adminUserMapper.deleteByPrimaryKey(userId);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        return result;
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param userId
     * 
     * @return
     * 
     * @throws CourtDatabaseException
     * 
     * @see com.bcinfo.alumni.service.BranchService#load(long)
     */
    @Override
    public AdminUser load(Long userId) throws PwtzDatabaseException
    {
        if (_logger.isInfoEnabled())
        {
            _logger.info("load:userId=" + userId);
        }
        AdminUser user = null;
        try
        {
            user = adminUserMapper.selectByPrimaryKey(userId);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        if (user != null)
        {
            _logger.debug("load:result user=" + user.toString());
        }
        return user;
    }
    
    @Override
    public AdminUser findUserInfo(String tel,String userPwd) throws PwtzDatabaseException
    {
        if (_logger.isInfoEnabled())
        {
            _logger.info("findUserInfo:tel=" + tel+"|userPwd=" + userPwd);
        }
        AdminUser user = null;
        try
        {
                user = adminUserMapper.selectUserByUserInfo(tel,userPwd);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        return user;
    }

    @Override
	public int update(AdminUser user) throws PwtzDatabaseException {
	    int result = 0;
	    if (_logger.isDebugEnabled())
        {
            _logger.debug("add:user=" + user.toString());
        }
        if (user == null)
        {
            if (_logger.isDebugEnabled())
            {
                _logger.debug("add,user is null");
            }
            return 0;
        }
        try
        {
            result=adminUserMapper.updateByPrimaryKeySelective(user);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
		return result;
	}
    
    @Override
    public AdminUser add(AdminUser user) throws PwtzDatabaseException
    {
        if (_logger.isDebugEnabled())
        {
            _logger.debug("register:user=" + user.toString());
        }
        if (user == null)
        {
            if (_logger.isDebugEnabled())
            {
                _logger.debug("register,user is null");
            }
            return null;
        }
        try
        {
            user.setCreateTime(new Date());
            adminUserMapper.insertSelective(user);
        }
        catch (DataAccessException _e)
        {
            _logger.error(ExceptionUtils.getStackTrace(_e));
            throw new PwtzDatabaseException();
        }
        return user;
    }
    
    
    @Override
    public int selectCountByName(String userName) throws PwtzDatabaseException
    {
        int result = 0;
        if (_logger.isInfoEnabled())
        {
            _logger.info("selectCountByName:userName=" + userName);
        }
        try
        {
            result = adminUserMapper.selectCountByName(userName);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        return result;
    }
    
    @Override
    public int selectCountByMobile(String mobile) throws PwtzDatabaseException
    {
        int result = 0;
        if (_logger.isInfoEnabled())
        {
            _logger.info("selectCountByName:mobile=" + mobile);
        }
        try
        {
            result = adminUserMapper.selectCountByMobile(mobile);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        return result;
    }
    
    @Override
    public int selectCountByPassword(Long adminUserId,String pwd) throws PwtzDatabaseException
    {
        int result = 0;
        if (_logger.isInfoEnabled())
        {
            _logger.info("selectCountByPassword=" + pwd);
        }
        try
        {
            result = adminUserMapper.selectCountByPassword(pwd, adminUserId);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        return result;
    }
    
    @Override
    public int updatePwdByMobile(String pwd,String mobile) throws PwtzDatabaseException
    {
        int result = 0;
        if (_logger.isInfoEnabled())
        {
            _logger.info("updatePwdByMobile:pwd=" + pwd + ",mobile=" + mobile);
        }
        try
        {
            result = adminUserMapper.updatePwdByMobile(pwd, mobile);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        return result;
    }

	@Override
	public PaginationList<AdminUser> findUser(Long nowAdminUserId,int pageSize, int pageNo)
			throws PwtzDatabaseException {
		PaginationList<AdminUser> paginationList = new PaginationList<AdminUser>();
		try {
			int beginIndex = pageSize * pageNo - pageSize;
			List<AdminUser> list = adminUserMapper.selectUserList(nowAdminUserId,beginIndex, pageSize);
			_logger.debug("AdminUserServiceImpl.findUser() result list = "+list);
			int totalCount = adminUserMapper.selectUserCount(nowAdminUserId);
			_logger.debug("AdminUserServiceImpl.findUser() result totalCount = "+list);
			int totalPage = (totalCount + pageSize -1)/pageSize;
			
			paginationList.setTotalPage(totalPage);
			paginationList.setCurrPage(pageNo);
			paginationList.setPageSize(pageSize);
			paginationList.setRecords(list);
			paginationList.setTotalRecord(totalCount);
		} catch (DataAccessException e) {
			_logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
		}
		return paginationList;
	}

	@Override
	public int deleteById(String id) throws PwtzDatabaseException {
		int row = 0;
		try {
			row = adminUserMapper.deleteById(id);
		} catch (DataAccessException e) {
			_logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
		}
		return row;
	}

}
